/**
 * 自动闭合处理器
 * 处理括号、引号等符号的自动闭合功能
 */
export class AutoCloseHandler {
    constructor(editor, options = {}) {
        this.editor = editor
        this.options = {
            autoCloseBrackets: true,
            autoCloseQuotes: true,
            ...options
        }
    }

    /**
     * 处理括号自动闭合
     * @param {Event} e - 键盘事件
     * @param {string} open - 开放括号
     * @param {string} close - 闭合括号
     * @returns {boolean} 是否处理了事件
     */
    handleAutoCloseBracket(e, open, close) {
        if (!this.options.autoCloseBrackets) return false

        e.preventDefault()
        const start = this.editor.editor.selectionStart
        const end = this.editor.editor.selectionEnd
        const selectedText = this.editor.editor.value.substring(start, end)

        this.editor.editor.value = 
            this.editor.editor.value.substring(0, start) +
            open + selectedText + close +
            this.editor.editor.value.substring(end)

        // 如果有选中文本，光标放在闭括号后；否则放在中间
        if (start !== end) {
            this.editor.editor.selectionStart = this.editor.editor.selectionEnd = end + 2
        } else {
            this.editor.editor.selectionStart = this.editor.editor.selectionEnd = start + 1
        }

        this.editor._updateEditor()
        return true
    }

    /**
     * 处理引号自动闭合
     * @param {Event} e - 键盘事件
     * @param {string} quote - 引号字符
     * @returns {boolean} 是否处理了事件
     */
    handleAutoCloseQuote(e, quote) {
        if (!this.options.autoCloseQuotes) return false

        e.preventDefault()
        const start = this.editor.editor.selectionStart
        const end = this.editor.editor.selectionEnd
        const selectedText = this.editor.editor.value.substring(start, end)

        this.editor.editor.value = 
            this.editor.editor.value.substring(0, start) +
            quote + selectedText + quote +
            this.editor.editor.value.substring(end)

        // 如果有选中文本，光标放在闭引号后；否则放在中间
        if (start !== end) {
            this.editor.editor.selectionStart = this.editor.editor.selectionEnd = end + 2
        } else {
            this.editor.editor.selectionStart = this.editor.editor.selectionEnd = start + 1
        }

        this.editor._updateEditor()
        return true
    }

    /**
     * 处理自动闭合相关的按键事件
     * @param {Event} e - 键盘事件
     * @returns {boolean} 是否处理了事件
     */
    handleKeyDown(e) {
        switch (e.key) {
            case '{':
                return this.handleAutoCloseBracket(e, '{', '}')
            case '[':
                return this.handleAutoCloseBracket(e, '[', ']')
            case '(':
                return this.handleAutoCloseBracket(e, '(', ')')
            case '"':
                return this.handleAutoCloseQuote(e, '"')
            case "'":
                return this.handleAutoCloseQuote(e, "'")
            case '`':
                return this.handleAutoCloseQuote(e, '`')
            default:
                return false
        }
    }

    /**
     * 更新配置
     * @param {Object} newOptions - 新的配置选项
     */
    updateOptions(newOptions) {
        this.options = { ...this.options, ...newOptions }
    }
}